<?php

namespace App\Utility;

use EasySwoole\Component\Process\AbstractProcess;
use EasySwoole\Queue\Job;
use Extend\Sign\SortSign;
use EasySwoole\EasySwoole\Config;
use EasySwoole\HttpClient\HttpClient;
use Extend\Log\OperationLog;

class QueueProcess extends AbstractProcess
{

    protected function run($arg)
    {

        // ./configure --with-php-config=/usr/local/php/bin/php-config --enable-openssl --enable-http2
        // \Swoole\Runtime::enableCoroutine();
        
        //获取配置
        $confInstance = Config::getInstance();
        $queueConf = $confInstance->getConf('queue');
        $signkey = $queueConf['signkey'] ?? '';
        //无消息时进入睡眠的时间，协程大于maxCurrency时进入睡眠的时间
        $breakTime = $queueConf['breakTime'] ?? 0.01;
        $waitTime = $queueConf['waitTime'] ?? 0.1;
        //最大协程数
        $maxCurrency = $queueConf['maxCurrency'] ?? 128;

        go(function () use ($signkey, $breakTime, $waitTime, $maxCurrency) {
            MyQueue::getInstance()->consumer()->listen(function (Job $job) use ($signkey){
                // \Swoole\Coroutine::sleep(.1);//协程切换
                // var_dump($job->toArray());
                // var_dump(time());
                $jobArr = $job->toArray();
                $jobData = $jobArr['jobData'] ?? [];
                $jobId = $jobArr['jobId'] ?? 0;

                // $host = $jobData['host'] ?? '';
                // $port = $jobData['port'] ?? '';
                // $ssl = $jobData['ssl'] ?? '';
                $url = $jobData['url'] ?? '';
                $params = $jobData['params'] ?? '';

                if(empty($url)){
                    return;
                }

                //http协程客户端
                // $ohttpClient = new \Swoole\Coroutine\Http\Client($host, $port, $ssl);

                $apiParams = [
                    'jobData' => json_encode($params),
                    'time'    => time(),
                ];

                //生成签名
                $sign = SortSign::makeSign($apiParams, $signkey);
                $apiParams['sign'] = $sign;

                $httpClient = new HttpClient($url);
                //设置等待超时时间
                $httpClient->setTimeout(30);
                //设置连接超时时间
                // $httpClient->setConnectTimeout(5);
                //设置是否开启ssl
                // $httpClient->setEnableSSL(false);

                $response = '';
                switch ($jobData['method']) {
                    case 'GET':
                        // $ohttpClient->get($jobData['path'].'?'.http_build_query($apiParams));
                        $httpClient->setQuery($apiParams);
                        $response = $httpClient->get();
                        break;
                    case 'POST':
                        // $ohttpClient->post($apiParams);
                        $response = $httpClient->post($apiParams);
                        break;
                }

                //接口返回值写入日志
                //记录日志
                OperationLog::addLog('queue_consume.log', [
                    'request' => [
                        'url' => $url,
                        'params' => $apiParams,
                    ],
                    'response' => [
                        'body' => $response->getBody(),
                        'errCode' => $response->getErrCode(),
                        'errMsg' => $response->getErrMsg(),
                        'statusCode' => $response->getStatusCode(),
                    ],
                ]);
                
                // var_dump($response->getBody());//获取响应主体
                // var_dump($response->getErrCode());//获取错误码
                // var_dump($response->getErrMsg());//获取错误信息
                // var_dump($response->getStatusCode());//获取响应状态码
                // $ohttpClient->close();

            }, $breakTime, $waitTime, $maxCurrency);
        });
    }
}
